package leetcode.editor.day;

import java.util.*;

// 662. 二叉树最大宽度
// https://leetcode.cn/problems/maximum-width-of-binary-tree/
class MaximumWidthOfBinaryTree {
    public static void main(String[] args) {
        Solution solution = new MaximumWidthOfBinaryTree().new Solution();
    }
    //leetcode submit region begin(Prohibit modification and deletion)


    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    class Solution {
        // TTL
        /*public int widthOfBinaryTree(TreeNode root) {
            Queue<TreeNode> queue = new LinkedList<>();
            queue.offer(root);
            int ans = 0;
            while (!queue.isEmpty()) {
                int size = queue.size();
                int width = 0, start = 0;
                boolean flag = false;
                for (int i = 0; i < size; i++) {
                    TreeNode node = queue.poll();
                    if (node != null) {
                        if (flag) {
                            width = i - start + 1;
                        } else {
                            width = 1;
                            start = i;
                        }
                        flag = true;
                        queue.offer(node.left);
                        queue.offer(node.right);
                    } else if (node == null && flag) {
                        queue.offer(null);
                        queue.offer(null);
                    }
                    ans = Math.max(ans, width);
                }
            }

            return ans;
        }*/

        // BFS
        // https://leetcode.cn/problems/maximum-width-of-binary-tree/solution/er-cha-shu-zui-da-kuan-du-by-leetcode-so-9zp3/
        // 进行编号，左子树为 2 * index，右子树为2 * index + 1
        /*public int widthOfBinaryTree(TreeNode root) {
            int res = 1;
            Deque<Pair<TreeNode, Integer>> queue = new LinkedList<>();
            queue.offer(new Pair<>(root, 1));
            while (!queue.isEmpty()) {
                // 双端队列头尾计算相差的编号
                int start = queue.peekFirst().getValue(), end = queue.peekLast().getValue();
                res = Math.max(res, end - start + 1);

                // 入队，修改编号
                int size = queue.size();
                for (int i = 0; i < size; i++) {
                    Pair<TreeNode, Integer> pair = queue.poll();
                    Integer index = pair.getValue();
                    TreeNode node = pair.getKey();

                    if (node.left != null) {
                        queue.offer(new Pair<>(node.left, 2 * index));
                    }

                    if (node.right != null) {
                        queue.offer(new Pair<>(node.right, 2 * index + 1));
                    }
                }
            }
            return res;
        }*/

        // DFS
        // https://leetcode.cn/problems/maximum-width-of-binary-tree/solution/er-cha-shu-zui-da-kuan-du-by-leetcode-so-9zp3/
        // 记录每层深度的最左边节点的编号
        Map<Integer, Integer> map = new HashMap<>();

        public int widthOfBinaryTree(TreeNode root) {
            return dfs(root, 1, 1);
        }

        public int dfs(TreeNode root, int depth, int index) {
            if (root == null) {
                return 0;
            }

            // 递归到最左边记录，不是最左边不进行记录
            map.putIfAbsent(depth, index);

            return Math.max(index - map.get(depth) + 1, Math.max(dfs(root.left, depth + 1, index * 2), dfs(root.right, depth + 1, index * 2 + 1)));
        }

    }
//leetcode submit region end(Prohibit modification and deletion)

}
